//
//  UIView+MMAdd.m
//  oc_demo
//
//  Created by haigui on 2021/8/5.
//

#import "UIView+MMAdd.h"

#import <objc/runtime.h>

static const char *key = "actionKey";

@implementation UIView (MMAdd)

- (CGFloat)x{
    return CGRectGetMinX(self.frame);
}
- (void)setX:(CGFloat)x{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}
- (CGFloat)left{
    return self.x;
}
- (void)setLeft:(CGFloat)left{
    self.x = left;
}



- (CGFloat)y{
    return CGRectGetMinY(self.frame);
}
- (void)setY:(CGFloat)y{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}
- (CGFloat)top{
    return self.y;
}
- (void)setTop:(CGFloat)top{
    self.y = top;
}



- (CGFloat)maxX{
    return CGRectGetMaxX(self.frame);
}
- (void)setMaxX:(CGFloat)maxX{
    CGRect frame = self.frame;
    frame.size.width = maxX - frame.origin.x;
    self.frame = frame;
}
- (CGFloat)right{
    return self.maxX;
}
- (void)setRight:(CGFloat)right{
    self.maxX = right;
}



- (CGFloat)maxY{
    return CGRectGetMaxY(self.frame);
}
- (void)setMaxY:(CGFloat)maxY {
    CGRect frame = self.frame;
    frame.size.height = maxY - frame.origin.y;
    self.frame = frame;
}
- (CGFloat)bottom{
    return self.maxY;
}
- (void)setBottom:(CGFloat)bottom{
    self.maxY = bottom;
}



- (CGFloat)width{
    return CGRectGetWidth(self.frame);
}
- (void)setWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)height{
    return CGRectGetHeight(self.frame);
}
- (void)setHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)centerX{
    return CGRectGetMidX(self.frame);
}
- (void)setCenterX:(CGFloat)centerX {
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}


- (CGFloat)centerY{
    return CGRectGetMidY(self.frame);
}
- (void)setCenterY:(CGFloat)centerY {
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

///添加点击事件
- (void)addTapGestureWithBlock:(void(^)(void))block{
    self.userInteractionEnabled = YES;
    
    objc_setAssociatedObject(self, key, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
    [self addGestureRecognizer:tap];
}
- (void)tapView:(UITapGestureRecognizer *)tap{
    void(^block)(void) = objc_getAssociatedObject(self, key);
    if (block) {
        block();
    }
}


///设置指定角为圆角
- (void)setRoundCornersIn:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii{
    
    //绘制圆角 要设置的圆角 使用“|”来组合
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:cornerRadii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    //设置大小
    maskLayer.frame = self.bounds;

    //设置图形样子
    maskLayer.path = maskPath.CGPath;

    self.layer.mask = maskLayer;
}

- (UIViewController *)viewController{
    UIResponder *responder = [self nextResponder];
    while (responder) {
        if ([responder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)responder;
        }
        responder = [responder nextResponder];
    }
    return nil;
}

@end
